package com.woniu.service.impl;

import com.woniu.entity.Permission;
import com.woniu.mapper.PermissionMapper;
import com.woniu.service.PermissionService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;
    @Override
    public ResponseUtil getAllPermissions() {
        try {
            List<Permission> permissions = permissionMapper.selectAllPermissions();
            if (permissions.isEmpty()) {
                return ResponseUtil.get(ResponseEnum.OK, "暂无权限数据");
            }

            // 按模块分组
            Map<String, List<Permission>> groupedPermissions = permissions.stream()
                    .collect(Collectors.groupingBy(Permission::getModule));

            return ResponseUtil.get(ResponseEnum.OK, groupedPermissions);  // 添加成功消息
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.get(ResponseEnum.FAIL, "查询权限列表失败");
        }
    }

    @Override
    public ResponseUtil getPermissionsByRoleId(Integer roleId) {
        try {
            if (roleId == null) {
                return ResponseUtil.get(ResponseEnum.FAIL, "角色ID不能为空");
            }

            List<Permission> permissions = permissionMapper.selectPermissionsByRoleId(roleId);
            // 返回权限ID列表，方便前端勾选
            List<Integer> permissionIds = permissions.stream()
                    .map(Permission::getId)
                    .collect(Collectors.toList());

            return ResponseUtil.get(ResponseEnum.OK,permissionIds);  // 添加成功消息
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.get(ResponseEnum.FAIL, "查询角色权限失败");
        }
    }

    @Override
    public ResponseUtil assignPermissionsToRole(Integer roleId, List<Integer> permissionIds) {
        try {
            if (roleId == null) {
                return ResponseUtil.get(ResponseEnum.FAIL,"角色ID不能为空");
            }

            // 删除原有权限
            permissionMapper.deleteRolePermissions(roleId);

            // 添加新权限
            if (permissionIds != null && !permissionIds.isEmpty()) {
                permissionMapper.insertRolePermissions(roleId, permissionIds);
            }

            return ResponseUtil.get(ResponseEnum.OK,"权限分配成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.get(ResponseEnum.FAIL,"权限分配失败");
        }
    }

    @Override
    public ResponseUtil checkUserPermission(Integer staffId, String permCode) {
        try {
            if (staffId == null) {
                return ResponseUtil.get(ResponseEnum.FAIL,"用户ID不能为空");
            }
            if (permCode == null || permCode.trim().isEmpty()) {
                return ResponseUtil.get(ResponseEnum.FAIL,"权限码不能为空");
            }

            int count = permissionMapper.checkUserPermission(staffId, permCode.trim());
            boolean hasPermission = count > 0;

            return ResponseUtil.get(ResponseEnum.OK, hasPermission);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.get(ResponseEnum.FAIL,"权限检查失败");
        }
    }
}
